#include "outsettingdlg.h"
#include "ui_outsettingdlg.h"
#include "iosettingwidget.h"
#include "outputfunction.h"
#include "common/common.h"
//#include <TSettings>
#include "common/tsettings/tsettings.h"

OutSettingDlg::OutSettingDlg(Output* output,QWidget *parent) :
    BaseEditor(QModelIndex(),parent),mOutput(output),
    ui(new Ui::OutSettingDlg)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    this->setGeometry(WINDOW_GEOMETRY_SHIFT(0.4,0.45,0));
    //QRegExp regexp("^[1-9][0-9]{1,5}$");
    QValidator *validator = new QIntValidator(0,999999);
    ui->le_continue->setValidator(validator);
    ui->le_delay->setValidator(validator);
    initDlg();
}

OutSettingDlg::~OutSettingDlg()
{
    delete ui;
}

void OutSettingDlg::initDlg()
{
    QStringList itemNameList = OutputFunction::getOutputFunctionMgr()->operationNameList();
    ui->cbxOp->addItems(itemNameList);
    ui->cbxOp->setCurrentIndex(mOutput->getOperation());
    QStringList normaStatusList = OutputFunction::getOutputFunctionMgr()->normalStatusList();
    ui->cbxNormal->addItems(normaStatusList);
    ui->cbxNormal->setCurrentIndex(mOutput->normalStatus());

    ui->le_delay->setText(QString::number(mOutput->getDelayTime()));
    ui->le_continue->setText(QString::number(mOutput->getContinueTime()));
}

void OutSettingDlg::on_btn_ok_clicked()
{
    uint no = mOutput->getNo();
    uint operation = ui->cbxOp->currentIndex();
    uint normalStatus = ui->cbxNormal->currentIndex();
    uint delay = ui->le_delay->text().toUInt();
    uint continueTime = ui->le_continue->text().toUInt();
    OutputFunction::getOutputFunctionMgr()->saveOutputFunction(no,normalStatus,operation,delay,continueTime);
//    TSettings set(DIR_SETTINGS,TSettings::IniFormat);
//    set.beginWriteArray(OutputFunction::OUTPUT_SET);
//    set.setArrayIndex(no);
//    set.setValue(OutputFunction::OPERATION,operation);
//    set.setValue(OutputFunction::NORMALSTATUS,normalStatus);
//    set.setValue(OutputFunction::DELAYTIME,delay);
//    set.setValue(OutputFunction::CONTINUETIME,continueTime);
//    set.endArray();
//    OutputFunction::getOutputFunctionMgr()->initOutputFunction(mOutput,operation,normalStatus,delay,continueTime);
    this->accept();
}

void OutSettingDlg::on_btn_cancel_clicked()
{
    this->reject();
}
